package cs5226;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Connect2DB {

	static Connection connection = null;
	static Connection connect() {
		try {
			// Load the JDBC driver
			String driverName = "oracle.jdbc.driver.OracleDriver";
			Class.forName(driverName);
			// ORACLE_SID=orcl
			// Create a connection to the database
			String serverName = "127.0.0.1";
			String portNumber = "1521";
			String sid = "xe";
			String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber
					+ ":" + sid;
			String username = "hr";
			String password = "040718";
			connection = DriverManager.getConnection(url, username, password);

			
			
		} catch (ClassNotFoundException e) {
			System.out.println("Could not find the database driver");
		} catch (SQLException e) {
			System.out.println("Could not connect to the database");
		}
		return connection;
	}
	
	public static void getUser(){
//		connect();
		Statement st =null;
		try {
			st = connection.createStatement();
			String sql = "select * from USER_DB where NAME = 'lixiaohui'";
			ResultSet rs = st.executeQuery(sql);
			// System.out.print("111");
			while (rs.next()) {
				System.out.println(rs.getString(1));
			}
			rs.close();
			st.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	public static void main(String[] args) {
		Connect2DB.connect();
		Connect2DB.getUser();
	}

}
